package com.canbot.u05.activity.admin

import android.app.Dialog
import android.os.Bundle
import android.text.TextUtils
import android.util.Base64
import android.view.View
import com.alibaba.fastjson.JSON
import com.canbot.u05.R
import com.canbot.u05.activity.BaseActivityNoTitle
import com.canbot.u05.activity.loge
import com.canbot.u05.activity.showToast
import com.canbot.u05.bean.AdminInfoBean
import com.canbot.u05.bean.JsonResponse
import com.canbot.u05.socket.IpMsgConst
import com.canbot.u05.utils.OkHttpUtils
import com.canbot.u05.widget.LoadingDialogUtils
import kotlinx.android.synthetic.main.activity_password_modify.*
import java.util.*

/**
 * desc: 管理员密码修改界面
 * author: xxl
 * date: 2019/6/19
 */
class PasswordModifyActivity : BaseActivityNoTitle() {
    private var adminInfoBean: AdminInfoBean? = null
    private var loadingDialog: Dialog? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_password_modify)

        val adminJson = intent.getStringExtra("admin")
        if (TextUtils.isEmpty(adminJson)) {
            getData()
        } else {
            adminInfoBean = JSON.parseObject(adminJson, AdminInfoBean::class.java)
        }

        findViewById<View>(R.id.btn_sure_modify_password).setOnClickListener {
            val newPassword = et_new_password.text.toString()
            val confirmPassword = et_confirm_password.text.toString()
            if (newPassword.isEmpty()) {
                showToast(getString(R.string.password_empty_tip))
                return@setOnClickListener
            }

            if (newPassword.length < 6) {
                showToast(getString(R.string.password_length_tip))
                return@setOnClickListener
            }

            if (confirmPassword != newPassword) {
                showToast(getString(R.string.twice_password_diffent_tip))
                return@setOnClickListener
            }

            updataPassword(newPassword)
        }
    }

    /**
     * 更新管理员密码
     * @param newPassword
     */
    private fun updataPassword(newPassword: String) {
        loadingDialog = LoadingDialogUtils.createLoadingDialog(this, getString(R.string.updating))
        if (adminInfoBean == null) {
            loge("更新失败,未获取到管理员信息")
            return
        }
        //密码加密后保存
        val encodePsw = Base64.encodeToString(newPassword.toByteArray(), Base64.DEFAULT)
        adminInfoBean!!.password = encodePsw
        val jsonString = JSON.toJSONString(adminInfoBean)
        val params = HashMap<String, String>()
        params["adminType"] = "updateAdmin"
        params["updateAdmin"] = jsonString
        params["type"] = "password"
        OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "admin", params, object : OkHttpUtils.OkHttpCallBack {
            override fun onSuccess(result: String) {
                loge("result:$result")
                val jsonResponse = JSON.parseObject(result, JsonResponse::class.java)
                if (jsonResponse.rc == -1) {//updateAdmin 更新失败
                    loge("updateAdmin 更新失败")
                    showToast(getString(R.string.update_failed))
                    runOnUiThread { cancelDialog() }

                } else if (jsonResponse.rc == 0) { //updateAdmin 更新成功
                    loge("updateAdmin 更新成功")
                    showToast(getString(R.string.update_successfully))
                    runOnUiThread {
                        cancelDialog()
                        finish()
                    }

                }
            }

            override fun onFailure() {

                loge("updateAdmin 更新Failure")
                showToast(getString(R.string.service_error_hint))
                runOnUiThread { cancelDialog() }
            }
        })

    }

    /**
     * 获取管理员信息
     */
    private fun getData() {
        val params = HashMap<String, String>()
        params["adminType"] = "queryAdmin"
        OkHttpUtils.upLoadFile("http://" + IpMsgConst.CHEST_SOCKET_HOST + ":7755/" + "admin", params, object : OkHttpUtils.OkHttpCallBack {
            override fun onSuccess(result: String) {
                val jsonResponse = JSON.parseObject(result, JsonResponse::class.java)
                if (jsonResponse.rc == 0) {
                    val jsonStr = jsonResponse.result.toString()
                    adminInfoBean = JSON.parseObject(jsonStr, AdminInfoBean::class.java)
                } else {
                    showToast(getString(R.string.admin_not_found))
                }
            }

            override fun onFailure() {
                showToast(getString(R.string.service_error_hint))
            }
        })
    }

    fun back(view: View) = finish()

    override fun onPause() {
        super.onPause()
        cancelDialog()
    }

    /**
     * 取消加载的Dialog
     */
    private fun cancelDialog() {
        if (loadingDialog != null && loadingDialog!!.isShowing) {
            loadingDialog?.cancel()
        }
    }



}
